/*
 * @Author: NJ_1098 xiabaoxin
 * @Date: 2025-02-11 14:21:36
 * @LastEditors: NJ_1098 xiabaoxin
 * @LastEditTime: 2025-02-14 09:33:52
 * @Description: 数据库连接
 */
const { Sequelize } = require("sequelize");
const logger = require("../log/log.js");
const {
  MYSQL_HOST,
  MYSQL_PORT,
  MYSQL_USER,
  MYSQL_PWD,
  MYSQL_DB,
} = require("../config/config.default");

/**
 * @author: NJ_1098 xiabaoxin
 * @description: 将sql语句输出到日志
 * @param {*} sql
 * @return {*}
 */
const sqlLog = (sql) => {
  logger.info(`log4j输出sql语句：\n ${sql}`);
};

const sequelize = new Sequelize(MYSQL_DB, MYSQL_USER, MYSQL_PWD, {
  dialect: "mysql",
  host: MYSQL_HOST,
  port: MYSQL_PORT,
  // 是否输出日志
  logging: sqlLog,
  // 东八区时间
  timezone: "+08:00",
});

/**
 * 测试数据库连接
 */
// sequelize
//   .authenticate()
//   .then((res) => {
//     console.log("数据库连接成功");
//   })
//   .catch((e) => {
//     console.log(e);
//   });

module.exports = sequelize;
